package com.yd.mvc.pc.jewellery.jewelleryPageController;

import com.yd.app.service.mobile.couponService.MCouponService;
import com.yd.app.service.mobile.orderService.MOrderService;
import com.yd.app.service.pc.jewellery.jewelleryPageService.WebSiteCartService;
import com.yd.app.service.pc.userCartService.UserCartService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.Order;
import com.yd.domain.business.OrderDetial;
import com.yd.domain.business.ShoppingCart;
import com.yd.domain.business.UserCoupon;
import com.yd.domain.sys.User;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zy
 * @Desc: 电脑端购物车
 * @Date:  2018/5/14
 * @Param
 */
@RestController
@RequestMapping(value = "/api/webSCart")
public class WebSiteShopingCartController {
    @Autowired
    private WebSiteCartService userCartService;
    @Autowired
    private MCouponService mCouponService;
    @Autowired
    private MOrderService orderService;
   /**
    * @Author: zy
    * @Desc: 商品加入购物车
    * @Date:  2018/5/14
    * @Param
    */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addUserCart(@RequestBody ShoppingCart shoppingCart)throws BusinessException{
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        shoppingCart.setUserId(user.getUserId());
        userCartService.addCart(shoppingCart);
    }
    /**
     * @Author: zy
     * @Desc: 查询购物车是否存在
     * @Date:  2018/4/14
     * @Param
     */
    @RequestMapping(value = "/getCartByUGId/{goodsId}/{goodsWeightId}", method = RequestMethod.GET)
    public ShoppingCart getCartByUGId(@PathVariable("goodsId") String goodsId,@PathVariable("goodsWeightId") String goodsWeightId,HttpSession session) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        ShoppingCart shoppingCart=userCartService.getCartByUGId(user.getUserId(),goodsId,goodsWeightId);
        return shoppingCart;
    }
    /**
     * @Author: zy
     * @Desc: 修改
     * @Date:  2018/5/14
     * @Param
     */
    @RequestMapping(value = "/updateNumber/{goodsId}/{number}", method = RequestMethod.GET)
    public void updateUserCartNumber(@PathVariable("goodsId") String goodsId,@PathVariable("number") int number){
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        userCartService.updateCartNumber(user.getUserId(),goodsId,number);
    }
    /**
     * @Author: zy
     * @Desc:删除
     * @Date:  2018/5/14
     * @Param
     */
    @RequestMapping(value = "/delete/{goodsId}/{goodsWeightId}", method = RequestMethod.GET)
    public void deleteUserCart( @PathVariable("goodsId") String goodsId,@PathVariable("goodsWeightId") String goodsWeightId,HttpSession session) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        userCartService.deleteCart(user.getUserId(), goodsId,goodsWeightId);
    }

    /**
     * 查询用户购物车信息
     *
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public List<ShoppingCart> getListCart(HttpSession session) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        List<ShoppingCart> list=new ArrayList<>();
        list = userCartService.getListCart(user.getUserId());
        return list;
    }
    /*---------------------------------优惠券----------------------------------*/
    /**
     * @Author: zy
     * @Desc: 查询优惠券
     * @Date:  2018/5/15
     * @Param
     */
    @RequestMapping(value = "/getUserCouponList", method = RequestMethod.GET)
    public List<UserCoupon> getCouponList(HttpSession session) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        List<UserCoupon> list = mCouponService.getCouponList(user.getUserId());
        for(int i =0;i<list.size();i++){
            if (list.get(i).getCoupon()==null) {
                list.remove(i);
            }
        }
        return list;
    }
    /*-------------------------------------添加订单----------------------------*/
    @RequestMapping(value = "/addOrder", method = RequestMethod.POST)
    public Order addOrder(@RequestBody OrderDetial orderDetial) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        Order order=null;
        order=orderService.addOrderDetail(orderDetial,user.getUserId(),user.getCompanyName(),user.getRealName());
        return order;
    }
}
